<?php
namespace  Teachers\Controller;
use Think\Controller;
class BasicInfoController extends CommonController
{
	public function index()
	{	
		$school_id=$this->showschoolID();
		if($school_id)
		{	
			$basic_Info=D('BasicInfo');
			$map['SchoolRelation.school_id']=$school_id;	
			$basic_data=$basic_Info->where($map)->select();		//基础学校属性
			$this->assign("basic_data",$basic_data);
			$school=M('School');
			$schoolmap['school_id']=$school_id;
			$school_data=$school->where($schoolmap)->find();
			$this->assign("school_data",$school_data);
			$address=M('SchoolAddress');
			$addressmap['school_id']=$school_id;
			$address_data=$address->where($addressmap)->select();
			$this->assign("address_data",$address_data);
			$this->display();
		}
	
	 
	}
	
	 /**
	  * 增加分校
	  * 
	  * */
	public function addAddress()
	{
		$school_id=$this->showschoolID();
		dump($school_id);
		if($school_id){
			$coordinate_data=array(
				'province'	=>	I('post.province_id'),	//省
				'city'		=>	I('post.city_id'),		//市
				'county'		=>	I('post.county_id'),	//县、区
			);
			$relation=M('school_relation');
			$map['school_id']=I('post.school_id',1);
			$map['school_type_id']=$this->getTypeId($coordinate_data['province'], 2);
			$province_relation_id=$relation->add($map);
			if(!$province_relation_id)
			{
				$this->error('保存出错!');
			}
			$map['school_type_id']=$this->getTypeId($coordinate_data['city'], 3);
			$city_relation_id=$relation->add($map);
			if(!$city_relation_id)
			{
				$this->error('保存出错!');
			}
			$map['school_type_id']=$this->getTypeId($coordinate_data['county'], 13);
			$county_relation_id=$relation->add($map);
			if(!$county_relation_id)
			{
				$this->error('保存出错!');
			}
			$data = array(			
				'school_id'				=> $school_id,
				'name'					=> I('post.name'),
				'school_address'		=> I('post.school_address'),
				'main'					=> I('post.main',0),
				'province_relation_id'	=> $province_relation_id,
				'city_relation_id'		=> $city_relation_id,
				'province'				=> $coordinate_data['province'],
				'city'					=> $coordinate_data['city'],
				'county'				=> $coordinate_data['county'],
				'county_relation_id'	=> $county_relation_id,
				'coordinate'			=> I('post.coordinate'),
				'zip'					=> I('post.zip'),
				'coordinate_x'					=> I('post.coordinate_x'),
				'coordinate_y'					=> I('post.coordinate_y')
			);
				$address=M('school_address');
				$flag=$address->add($data);	
				$params=array(
					'school_id'	=>	I('post.school_id'),
				);
				if($flag){
					$this->redirect('Teachers/BasicInfo/index',$params, 0, '页面跳转中...');
				}
		}
	}
	
	
	
	/**
	 * 编辑分校
	 * 
	 * 
	 * 
	 * */
	public function updateAddress()
	{
		$school_id=$this->showschoolID();
		if($school_id)
		{
			$coordinate_data=array(
				'province'	=>	I('post.province_id'),	//省
				'city'		=>	I('post.city_id'),		//市
				'county'		=>	I('post.county_id'),	//县、区
				'school'		=>	$school_id,
				'address'	=>	I('post.address_id')
			);
			$relation=M('school_relation');
			$address=D('Address');
			$mapwhere['school_id']=$school_id;
			$mapwhere['address_id']=$coordinate_data['address'];
			$relation_id=$address->where($mapwhere)->find();
			$data['school_type_id']=$this->getTypeId($coordinate_data['province'], 2);
			$map['school_relation_id']=$relation_id['province_relation_id'];
			$flag_pro=$relation->where($map)->save($data);	//更新省份
			/*if(isset($flag_pro))
			{
				$this->error('更新出错1！');
			}*/
			$data['school_type_id']=$this->getTypeId($coordinate_data['city'], 3);
			$map['school_relation_id']=$relation_id['city_relation_id'];
			$flag_city=$relation->where($map)->save($data);//更新市
			/*if(isset($flag_city))
			{
				$this->error('更新出错2！');
			}*/
			$data['school_type_id']=$this->getTypeId($coordinate_data['county'], 13);
			$map['school_relation_id']=$relation_id['county_relation_id'];
			$flag_county=$relation->where($map)->save($data);//更新县区
			/*if(isset($flag_county))
			{
				$this->error('更新出错3！');
			}*/
			$address_data=array(
				'name'					=> I('post.name'),
				'school_address'		=> I('post.school_address'),
				'coordinate'			=> I('post.coordinate'),
				'province'				=> $coordinate_data['province'],
				'city'					=> $coordinate_data['city'],
				'county'				=> $coordinate_data['county'],
				'zip'					=> I('post.zip'),
				'coordinate_x'			=> I('post.coordinate_x'),
				'coordinate_y'			=> I('post.coordinate_y')
			);
			unset($map);
			$map['address_id']=I('post.address_id');
			$map['school_id']=$school_id;
			$address=M('school_address');
			$flag=$address->where($map)->save($address_data);
			$this->redirect('Teachers/BasicInfo/index',$params, 0, '页面跳转中...');
		}
	}
	
	/**
	 * 删除分校
	 * 
	 * */
	
	public function deleteAddress()
	{
		$address=M('SchoolAddress');
		$relation=M('SchoolRelation');
		$map['address_id']=I('post.address_id');
		if($map['address_id']){
			$address_data=$address->where($map)->find();
			$relation_id=$address_data['province_relation_id'].','.$address_data['city_relation_id'].','.$address_data['county_relation_id'];
			$flag=$address->delete($map['address_id']);
			$flagrelation=$relation->delete($relation_id);
		}
		$this->redirect('Teachers/BasicInfo/index',$params, 0, '页面跳转中...');
	}
	
	
	
	/**
	 *设置主校区 
	 */
	public function setMain(){
		$school_id=$this->showschoolID();
		$address_id=I('post.address_id');
		if($address_id){
			$data=array(
				'main'	=>	0,
			);
			$address=M('SchoolAddress');
			$flag=$address->where($map)->save($data);	//先置所有校区不为主校区
			if(isset($flag)){									
				$map['address_id']=$address_id;
				unset($data);
				$data=array(
					'main'	=>	1,
				);
				$flag=$address->where($map)->save($data);//再置当前操作校区为主校区
			}
		}
		if(!$flag){
			$this->error('设为主校区出错！');
		}
	
	}
	
	
	/**
	 * 取得当前属性的type_id
	 * 
	 * 有时返回ID 没有则新增后返回ID
	 * 
	 * */
	public function getTypeId($type_name,$type_sort){
		$type=M('SchoolType');
		if(isset($type_name)){
			$map['school_type_name']=$type_name;
		}
		if(isset($type_sort)){
			$map['school_type_sort']=$type_sort;
		}
		$typedata=$type->where($map)->find();
		if(isset($typedata)){
			return $typedata['school_type_id'];
		}else{
			$data=array(
				'school_type_name'	=>	$type_name,
				'school_type_sort'	=>	$type_sort
			);
			$id=$type->add($data);
			if(isset($id)){
				return $id;
			}else{
				$this->error('获取属性出错！');
			}
		}
		
	}


}